package com.bigegg.employee.controller;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bigegg.employee.util.CommonUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.bigegg.employee.domain.Assistant;
import com.bigegg.employee.service.IAssistantService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 辅导员Controller
 * 
 * @author 刘博
 * @date 2022-08-17
 */
@RestController
@RequestMapping("/employee/assistant")
public class AssistantController extends BaseController
{
    @Autowired
    private IAssistantService assistantService;

    /**
     * 查询辅导员列表
     */
    @PreAuthorize("@ss.hasPermi('employee:assistant:list')")
    @GetMapping("/list")
    public TableDataInfo list(Assistant assistant)
    {
        startPage();
        List<Assistant> list = assistantService.selectAssistantList(assistant);
        return getDataTable(list);
    }

    /**
     * 导出辅导员列表
     */
    @PreAuthorize("@ss.hasPermi('employee:assistant:export')")
    @Log(title = "辅导员", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Assistant assistant)
    {
        List<Assistant> list = assistantService.selectAssistantList(assistant);
        ExcelUtil<Assistant> util = new ExcelUtil<Assistant>(Assistant.class);
        util.exportExcel(response, list, "辅导员数据");
    }

    /**
     * 获取辅导员详细信息
     */
    @PreAuthorize("@ss.hasPermi('employee:assistant:query')")
    @GetMapping(value = "/{userId}")
    public AjaxResult getInfo(@PathVariable("userId") Long userId)
    {
        return AjaxResult.success(assistantService.selectAssistantByUserId(userId));
    }

    /**
     * 新增辅导员
     */
    @PreAuthorize("@ss.hasPermi('employee:assistant:add')")
    @Log(title = "辅导员", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Assistant assistant)
    {
        return toAjax(assistantService.insertAssistant(assistant));
    }

    /**
     * 修改辅导员
     */
    @PreAuthorize("@ss.hasPermi('employee:assistant:edit')")
    @Log(title = "辅导员", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Assistant assistant)
    {
        return toAjax(assistantService.updateAssistant(assistant));
    }

    /**
     * 删除辅导员
     */
    @PreAuthorize("@ss.hasPermi('employee:assistant:remove')")
    @Log(title = "辅导员", businessType = BusinessType.DELETE)
	@DeleteMapping("/{userIds}")
    public AjaxResult remove(@PathVariable Long[] userIds)
    {
        return toAjax(assistantService.deleteAssistantByUserIds(userIds));
    }

    /**
     * 上传excel
     */
    @PostMapping("/importData")
    public AjaxResult importData(@RequestParam("file") MultipartFile file)
    {

        this.assistantService.importData(file);
        return AjaxResult.success();
    }

    /**
     * 下载模板
     */
    @PostMapping("/downloadTpl")
    public void downloadTpl(HttpServletRequest request, HttpServletResponse response)
    {

        // 模板的路径
        String tplPath = "tpl/辅导员导入模板.xlsx";

        CommonUtil.downloadResource(tplPath,response);

    }
}
